﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cuce.ReportingServices.SSRSWS;

namespace Cuce.ReportingServices.UnitTests.Mock
{
    /// <summary>
    /// 
    /// </summary>
    public class ReportingServicesMock : IReportingService
    {
        #region IReportingService Members

        /// <summary>
        /// Gets the report definition.
        /// </summary>
        /// <param name="Report">The report.</param>
        /// <returns></returns>
        public byte[] GetReportDefinition(string Report)
        {
            if (Report.Contains("warning"))
            {
                throw new Exception("Some warning");
            }

            if (Report.Contains("error"))
            {
                throw new Exception("Some Error");
            }
            
            
            var encoding = new ASCIIEncoding();
            return encoding.GetBytes(Report);
           
        }

        /// <summary>
        /// Deletes the item.
        /// </summary>
        /// <param name="item">The item.</param>
        public void DeleteItem(string item)
        {
            if (item.Contains("warning"))
            {
                throw new Exception("Some warning");
            }

            if (item.Contains("error"))
            {
                throw new Exception("Some Error");
            }

        }
        
        /// <summary>
        /// Creates the report.
        /// </summary>
        /// <param name="Report">The report.</param>
        /// <param name="Parent">The parent.</param>
        /// <param name="Overwrite">if set to <c>true</c> [overwrite].</param>
        /// <param name="Definition">The definition.</param>
        /// <param name="Properties">The properties.</param>
        /// <returns></returns>
        public Warning[] CreateReport(string Report, string Parent, bool Overwrite, byte[] Definition, Property[] Properties)
        {
           
            if (Report.ToLower() == "warning")
            {
                var Warnings = new Warning[1];
                Warnings[0] = new Warning {Message = Report};

                return Warnings;
            }

            if (Report.ToLower() == "error")
            {
                throw new Exception("Some Error");
            }

            return null;
        
        }


        /// <summary>
        /// Lists the roles.
        /// </summary>
        /// <returns></returns>
        public Role[] ListRoles() { return null; }

        /// <summary>
        /// Gets the report data sources.
        /// </summary>
        /// <param name="Report">The report.</param>
        /// <returns></returns>
        public DataSource[] GetReportDataSources(string Report)
        {
            var list = new DataSource[1];

            return list;
        }

        /// <summary>
        /// Renders the specified report.
        /// </summary>
        /// <param name="Report">The report.</param>
        /// <param name="Format">The format.</param>
        /// <param name="HistoryID">The history ID.</param>
        /// <param name="DeviceInfo">The device info.</param>
        /// <param name="Parameters">The parameters.</param>
        /// <param name="Credentials">The credentials.</param>
        /// <param name="ShowHideToggle">The show hide toggle.</param>
        /// <param name="Encoding">The encoding.</param>
        /// <param name="MimeType">Type of the MIME.</param>
        /// <param name="ParametersUsed">The parameters used.</param>
        /// <param name="Warnings">The warnings.</param>
        /// <param name="StreamIds">The stream ids.</param>
        /// <returns></returns>
        public byte[] Render(string Report, string Format, string HistoryID, string DeviceInfo, ParameterValue[] Parameters, DataSourceCredentials[] Credentials, string ShowHideToggle, out string Encoding, out string MimeType, out ParameterValue[] ParametersUsed, out Warning[] Warnings, out string[] StreamIds)
        {
            var list = new byte[1];

            Encoding = "";
            MimeType = "";
            ParametersUsed = null;
            Warnings = null;
            StreamIds = null;

            if (Parameters.Length > 0)
            {
                if (Parameters[0].Name == "Error")
                    throw new Exception("Fake Web Exception");
                
                if (Parameters[0].Name == "Warning")
                {
                    Warnings = new Warning[1];
                    Warnings[0] = new Warning {Message = Parameters[0].Name};
                }
            }

            if (Report == "/testreport")
            {
                //create test string, convert into byte array and return
                list = StrToByteArray("test, test, test, test\ntest, test, test, test");
            }
            else
            {
                //create test string, convert into byte array and return
                list = StrToByteArray(Report);
            }


            return list;
        }

        private static byte[] StrToByteArray(string str)
        {
            var encoding = new System.Text.ASCIIEncoding();
            return encoding.GetBytes(str);
        }

        /// <summary>
        /// Sets the report data sources.
        /// </summary>
        /// <param name="Report">The report.</param>
        /// <param name="DataSources">The data sources.</param>
        public void SetReportDataSources(string Report, DataSource[] DataSources)
        {
            return;
        }

        /// <summary>
        /// Gets or sets the URL.
        /// </summary>
        /// <value>The URL.</value>
        public string Url {get; set;}
        
      

        

        /// <summary>
        /// Gets or sets the proxy.
        /// </summary>
        /// <value>The proxy.</value>
        public System.Net.IWebProxy Proxy
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the credentials.
        /// </summary>
        /// <value>The credentials.</value>
        public System.Net.ICredentials Credentials
        {
            get;
            set;
        }

        #endregion
    }
}
